package com.hsq.androidproject.ui;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.MyFragmentPagerAdapter;
import com.hsq.androidproject.entity.rtv;
import com.hsq.androidproject.entity.rtvdetails;
import com.hsq.androidproject.ui.recommend_fragment.rTv.DramaFragment;
import com.hsq.androidproject.ui.recommend_fragment.rTv.IntroductionFragment;
import com.hsq.androidproject.utils.OkHttpClientManager;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.List;

public class rtvActivity extends FragmentActivity {
    TextView name, jishu, source, tv_rank;
    ImageView img;

    rtv r = new rtv();
    private RatingBar mRatingBar;
    private Button btn_play, btn_down;
    private Drawable drawablerb_play;
    private Drawable drawablerb_down;
    ////////viewpager
    private Fragment dramaFragment, introductionFragment;
    private ViewPager mPager;
    private ArrayList<Fragment> fragmentList;
    private TextView barText;
    private TextView view1, view2;
    private int currIndex;//当前页卡编号

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rtv);
        initView();
        Intent intent = getIntent();
        r = (rtv) intent.getSerializableExtra("rtvbean");
        if (r.getIsFinished().equals("true")) {
            jishu.setText(r.getCount() + "完");
        } else {
            jishu.setText("更新至" + r.getCount() + "集");
        }
        source.setText("来源: " + r.getSource());
        Glide.with(this)
                .load(r.getBitmap())
                .into(img);
        r.getRank();

        name.setText(r.getName() + "");

        float count = (float) (r.getRank() * 0.1);
        tv_rank.setText(count + "");
        float countSee = count / 2;
        mRatingBar.setRating(countSee);

        drawablerb_play = getResources().getDrawable(R.drawable.video_bottom_play);
        drawablerb_play.setBounds(40, 0, 70, 40);//第一0是距左右边距离，第二0是距上下边距离，第三宽度,第四高度
        btn_play.setCompoundDrawables(drawablerb_play, null, null, null);//只放上面/////

        drawablerb_down = getResources().getDrawable(R.drawable.download_icon);
        drawablerb_down.setBounds(40, 0, 70, 40);//第一0是距左右边距离，第二0是距上下边距离，第三宽度,第四高度
        btn_down.setCompoundDrawables(drawablerb_down, null, null, null);//只放上面/////
        btn_play.setOnClickListener(new View.OnClickListener() {            /////点击播放，播放第一集
            @Override
            public void onClick(View v) {
                String url = "http://api.hanju.koudaibaobao.com/api/series/detailV3?sid=" + r.getId();
                OkHttpClientManager.getAsyn(url, new OkHttpClientManager.ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        rtvdetails rtvdetails1 = new Gson().fromJson(response, rtvdetails.class);
                        List<rtvdetails.PlayItemsBean> playItemsBean = rtvdetails1.getPlayItems();
                        String path = "http://api.hanju.koudaibaobao.com/api/series/itemRefV3?pid=" + playItemsBean.get(0).getPid();
                        Intent intent = new Intent(rtvActivity.this, VideoViewActivity.class);
                        intent.putExtra("path", path);
                        startActivity(intent);
                    }
                });
            }
        });
        btn_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(rtvActivity.this, "该功能在测试阶段，仅对部分用户开启，敬请期待！！！", Toast.LENGTH_SHORT).show();
            }
        });
        InitTextView();
        InitTextBar();
        InitViewPager(r.getId());
    }

    private void InitViewPager(String sid) {
        mPager = (ViewPager) findViewById(R.id.viewpager);
        fragmentList = new ArrayList<>();/////detailsFragment, selectionsFragment
        dramaFragment = new DramaFragment();        ///////详情Fragment--传输过去的数据
        Bundle bundle = new Bundle();
        bundle.putString("id", sid);
        dramaFragment.setArguments(bundle);

        introductionFragment = new IntroductionFragment(); //////选集Fragment--传输过去的数据
        Bundle bundle1 = new Bundle();
        bundle.putString("id", sid);
        introductionFragment.setArguments(bundle1);

        fragmentList.add(dramaFragment);
        fragmentList.add(introductionFragment);


        //给ViewPager设置适配器
        mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
        mPager.setCurrentItem(0);//设置当前显示标签页为第一页
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());//页面变化时的监听器
    }

    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // 取得该控件的实例
            LinearLayout.LayoutParams ll = (android.widget.LinearLayout.LayoutParams) barText
                    .getLayoutParams();

            if (currIndex == arg0) {
                ll.leftMargin = (int) (currIndex * barText.getWidth() + arg1
                        * barText.getWidth());
            } else if (currIndex > arg0) {
                ll.leftMargin = (int) (currIndex * barText.getWidth() - (1 - arg1) * barText.getWidth());
            }
            barText.setLayoutParams(ll);
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageSelected(int arg0) {
            currIndex = arg0;
            int i = currIndex + 1;
            /*Toast.makeText(getContext(), "您选择了第" + i + "个页卡", Toast.LENGTH_SHORT).show();*/
        }
    }

    private void InitTextBar() {
        barText = (TextView) findViewById(R.id.cursor);
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        // 得到显示屏宽度
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        // 1/3屏幕宽度
        int tabLineLength = metrics.widthPixels / 2;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) barText.getLayoutParams();
        lp.width = tabLineLength;
        barText.setLayoutParams(lp);
    }

    private void InitTextView() {
        view1 = (TextView) findViewById(R.id.tv_guid1);
        view2 = (TextView) findViewById(R.id.tv_guid2);


        view1.setOnClickListener(new txListener(0));
        view2.setOnClickListener(new txListener(1));
    }

    public class txListener implements View.OnClickListener {
        private int index = 0;

        public txListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    }

    private void initView() {
        name = (TextView) findViewById(R.id.rtvname);
        jishu = (TextView) findViewById(R.id.rtvjishu);
        source = (TextView) findViewById(R.id.source);
        img = (ImageView) findViewById(R.id.rtv_img);
        tv_rank = (TextView) findViewById(R.id.tv_rank);
        mRatingBar = (RatingBar) findViewById(R.id.ratingbar);
        btn_play = (Button) findViewById(R.id.btn_play);
        btn_down = (Button) findViewById(R.id.btn_down);
    }

    public void clickfinnsh(View view) {
        finish();
    }
}
